##########################################
### LANE AND SCHOENHERR CITE AND SWAY? ###
##########################################

# Purpose:
# 	- Replicate Tables 1, 2, and 3 in manuscript
# 	- Replicate Tables A1 - A11 in the supplemental appendix

# Data Sources:
#	- Analysis1Data.csb
#	- Analysis1IssueAreaData.csv

# Run on:
#	- R 4.4.0 ("Puppy Cup") on macOS Sequoia 15.1.1.

# Associated codebook:
#	- Analysis1Codebook.pdf

###########################################
###########################################
###########################################

library(foreign)
library(arm)
library(readxl)
library(lattice)
library(tidyverse)
library(stargazer)
library(faraway)
library(lme4)

####################################
## STEP 0: SET WORKING DIRECTORY ###
####################################

setwd()

########################################
### STEP 1: PULL IN THE MODEL 1 DATA ###
########################################

model1data <- read.csv("Analysis1Data.csv")

############################################
### STEP 2: PRE-PROCESS SOME OF THE DATA ###
############################################

model1data$logAttorneyBriefExperience <- log(model1data$attorneyBriefExperience + 1)
model1data$logTotalNumCites <- log(model1data$totalNumCites + 1)

# check all the variables to make sure no NAs before running
table(is.na(model1data$unnamedCiteCount))
table(is.na(model1data$majCallOutCount))
table(is.na(model1data$notMajOpinCount))
table(is.na(model1data$median))
table(is.na(model1data$ideoAlign))
table(is.na(model1data$logAttorneyBriefExperience))
table(is.na(model1data$partySG))
table(is.na(model1data$partyStatus))
table(is.na(model1data$logTotalNumCites))
table(is.na(model1data$pastExpertise))
table(is.na(model1data$clerkPeriod))
table(is.na(model1data$clerkedForThisJustice))

model1data$otherCites <- model1data$totalNumCites - model1data$unnamedCiteCount
model1data$logOtherCites <- log(model1data$otherCites + 1)

model1data$activeCiteCount <- model1data$majCallOutCount + model1data$notMajOpinCount
model1data$logActiveCites <- log(model1data$activeCiteCount + 1)

############################################
### STEP 3: SPLIT INTO PET AND RESP DATA ###
############################################

# because the strategies are different due to timing and boundaries on respondent (who won at lower court, see Wedeking 2010)

model1dataPet <- model1data %>% filter(pet == 1)
model1dataResp <- model1data %>% filter(pet == 0)

#################################
### STEP 4A: PET MODELS IN MS ###
#################################

### TABLE 2, COLUMN 1 ###
model1passivePet <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1passivePet)
display(model1passivePet)

### TABLE 2, COLUMN 2 ###
model1majPet <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1majPet)
display(model1majPet)

### TABLE 2, COLUMN 3 ###
model1separatePet <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1separatePet)
display(model1separatePet)

### TABLE 2 PULL ###
#stargazer(model1passivePet, model1majPet, model1separatePet, align = TRUE, omit.stat=c("LL", "ser", "f"))

##################################
### STEP 4B: RESP MODELS IN MS ###
##################################


### TABLE 3, COLUMN 1 ###
model1passiveResp <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1passiveResp)
display(model1passiveResp)

### TABLE 3, COLUMN 2 ###
model1majResp <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1majResp)
display(model1majResp)

### TABLE 3, COLUMN 3 ###
model1separateResp <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1separateResp)
display(model1separateResp)

### TABLE 3 PULL ###
#stargazer(model1passiveResp, model1majResp, model1separateResp, align = TRUE, omit.stat=c("LL", "ser", "f"))

#########################################
### STEP 5: SUMMARY STATS FOR TABLE 1 ###
#########################################

### MANUSCRIPT TABLE 1 ###

summary(model1dataPet$unnamedCiteCount)
summary(model1dataPet$majCallOutCount)
summary(model1dataPet$notMajOpinCount)
summary(model1dataResp$unnamedCiteCount)
summary(model1dataResp$majCallOutCount)
summary(model1dataResp$notMajOpinCount)

##################################
### STEP 6: ISSUE-AREA MEDIANS ###
##################################

# Tables A1 and A2 in supplemental appendix

## using a different dataset for this, so pull it in
model1issue <- read.csv("Analysis1IssueAreaData.csv")

model1issue$logAttorneyBriefExperience <- log(model1issue$attorneyBriefExperience + 1)
model1issue$logTotalNumCites <- log(model1issue$totalNumCites + 1)

# check all the variables to make sure no NAs before running
table(is.na(model1issue$unnamedCiteCount))
table(is.na(model1issue$majCallOutCount))
table(is.na(model1issue$notMajOpinCount))
table(is.na(model1issue$issueIdeoAlign))
table(is.na(model1issue$ideoAlign))
table(is.na(model1issue$logAttorneyBriefExperience))
table(is.na(model1issue$partySG))
table(is.na(model1issue$partyStatus))
table(is.na(model1issue$logTotalNumCites))
table(is.na(model1issue$pastExpertise))
table(is.na(model1issue$clerkPeriod))
table(is.na(model1issue$clerkedForThisJustice))

model1data$otherCites <- model1data$totalNumCites - model1data$unnamedCiteCount
model1data$logOtherCites <- log(model1data$otherCites + 1)

model1data$activeCiteCount <- model1data$majCallOutCount + model1data$notMajOpinCount
model1data$logActiveCites <- log(model1data$activeCiteCount + 1)

# split the data 
model1issuePet <- model1issue %>% filter(pet == 1)
model1issueResp <- model1issue %>% filter(pet == 0)

##################################
### STEP 6A: PET ISSUE MEDIANS ###
##################################

### TABLE A1, COLUMN 1 ###
model1passivePetIssueMedian <- lmer(unnamedCiteCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issuePet)
summary(model1passivePetIssueMedian)
display(model1passivePetIssueMedian)

### TABLE A1, COLUMN 2 ###
model1majPetIssueMedian <- lmer(majCallOutCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issuePet)
summary(model1majPetIssueMedian)
display(model1majPetIssueMedian)

### TABLE A1, COLUMN 3 ###
model1separatePetIssueMedian <- lmer(notMajOpinCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issuePet)
summary(model1separatePetIssueMedian)
display(model1separatePetIssueMedian)

### TABLE A1 PULL ###
#stargazer(model1passivePetIssueMedian, model1majPetIssueMedian, model1separatePetIssueMedian, align = TRUE, omit.stat=c("LL", "ser", "f"))

###################################
### STEP 6B: RESP ISSUE MEDIANS ###
###################################

### TABLE A2, COLUMN 1 ###
model1passiveRespIssueMedian <- lmer(unnamedCiteCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issueResp)
summary(model1passiveRespIssueMedian)
display(model1passiveRespIssueMedian)

### TABLE A2, COLUMN 2 ###
model1majRespIssueMedian <- lmer(majCallOutCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issueResp)
summary(model1majRespIssueMedian)
display(model1majRespIssueMedian)

### TABLE A2, COLUMN 3 ###
model1separateRespIssueMedian <- lmer(notMajOpinCount ~ issueMedian
						+ issueIdeoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1issueResp)
summary(model1separateRespIssueMedian)
display(model1separateRespIssueMedian)

### TABLE A2 PULL ###
#stargazer(model1passiveRespIssueMedian, model1majRespIssueMedian, model1separateRespIssueMedian, align = TRUE, omit.stat=c("LL", "ser", "f"))

########################################
### STEP 7: CONTROLING FOR CITATIONS ###
########################################

# Tables A3 and A4 in the Supplemental Appendix

###############################
### STEP 7A: PET OTHER CITE ###
###############################

model1dataPet$totalOtherCitesUnnamed <- model1dataPet$totalNumCites - model1dataPet$unnamedCiteCount
model1dataPet$logTotalOtherCitesUnnamed <- log(model1dataPet$totalOtherCitesUnnamed + 1)

### TABLE A3, COLUMN 1 ###
model1activePetAltCites <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalOtherCitesUnnamed
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1activePetAltCites)
display(model1activePetAltCites)

model1dataPet$totalOtherCitesMajCallOut <- model1dataPet$totalNumCites - model1dataPet$majCallOutCount
model1dataPet$logTotalOtherCitesMajCallOut <- log(model1dataPet$totalOtherCitesMajCallOut + 1)

### TABLE A3, COLUMN 2 ###
model1majPetAltCite <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ partyStatus
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ logTotalOtherCitesMajCallOut
						+ pastExpertise
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1majPetAltCite)
display(model1majPetAltCite)

model1dataPet$totalOtherCitesNotMaj <- model1dataPet$totalNumCites - model1dataPet$notMajOpinCount
model1dataPet$logTotalOtherCitesNotMaj <- log(model1dataPet$totalOtherCitesNotMaj + 1)

### TABLE A3, COLUMN 3 ###
model1separatePetAltCite <- lmer(notMajOpinCount ~median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalOtherCitesNotMaj
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1separatePetAltCite)
display(model1separatePetAltCite)

### TABLE A3 PULL ###
#stargazer(model1activePetAltCites, model1majPetAltCite, model1separatePetAltCite, align = TRUE, omit.stat=c("LL", "ser", "f"))

################################
### STEP 7B: RESP OTHER CITE ###
################################

### TABLE A4, COLUMN 1 ###
model1dataResp$totalOtherCitesUnnamed <- model1dataResp$totalNumCites - model1dataResp$unnamedCiteCount
model1dataResp$logTotalOtherCitesUnnamed <- log(model1dataResp$totalOtherCitesUnnamed + 1)

model1activeRespAltCites <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalOtherCitesUnnamed
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1activeRespAltCites)
display(model1activeRespAltCites)

model1dataResp$totalOtherCitesMajCallOut <- model1dataResp$totalNumCites - model1dataResp$majCallOutCount
model1dataResp$logTotalOtherCitesMajCallOut <- log(model1dataResp$totalOtherCitesMajCallOut + 1)

### TABLE A4, COLUMN 2 ###
model1majRespAltCite <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalOtherCitesMajCallOut
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1majRespAltCite)
display(model1majRespAltCite)

model1dataResp$totalOtherCitesNotMaj <- model1dataResp$totalNumCites - model1dataResp$notMajOpinCount
model1dataResp$logTotalOtherCitesNotMaj <- log(model1dataResp$totalOtherCitesNotMaj + 1)

### TABLE A4, COLUMN 3 ###
model1separateRespAltCite <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalOtherCitesNotMaj
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1separateRespAltCite)
display(model1separateRespAltCite)

### TABLE A4 PULL ###
stargazer(model1activeRespAltCites, model1majRespAltCite, model1separateRespAltCite, align = TRUE, omit.stat=c("LL", "ser", "f"))

##################################
### STEP 8: OSG and CSI Models ###
##################################

# Tables A5 and A6 in the Supplemental Appendix

################################
### STEP 8A: PET OSG AND CSI ###
################################

table(is.na(model1data$CSI))

### TABLE A5, COLUMN 1 ###
model1activePetKitchen <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1activePetKitchen)
display(model1activePetKitchen)

### TABLE A5, COLUMN 2 ###
model1majPetKitchen <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1majPetKitchen)
display(model1majPetKitchen)

### TABLE A5, COLUMN 3 ###
model1separatePetKitchen <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1separatePetKitchen)
display(model1separatePetKitchen)

### TABLE A5 PULL ###
#stargazer(model1activePetKitchen, model1majPetKitchen, model1separatePetKitchen, align = TRUE, omit.stat=c("LL", "ser", "f"))

#################################
### STEP 8B: RESP OSG AND CSI ###
#################################

### TABLE A6, COLUMN 1 ###
model1activeRespKitchen <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1activeRespKitchen)
display(model1activeRespKitchen)

### TABLE A6, COLUMN 2 ###
model1majRespKitchen <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1majRespKitchen)
display(model1majRespKitchen)

### TABLE A6, COLUMN 3 ###
model1separateRespKitchen <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ partySG
						+ CSI
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1separateRespKitchen)
display(model1separateRespKitchen)

### TABLE A6 PULL ###
#stargazer(model1activeRespKitchen, model1majRespKitchen, model1separateRespKitchen, align = TRUE, omit.stat=c("LL", "ser", "f"))

###################################
### STEP 9: VARIABLE BREAKDOWNS ###
###################################

### TABLE A7 ###
summary(model1data$unnamedCiteCount)
summary(model1data$majCallOutCount)
summary(model1data$notMajOpinCount)
summary(model1data$median)
summary(model1data$ideoAlign)
summary(model1data$pastExpertise)
summary(model1data$logAttorneyBriefExperience)
summary(model1data$clerkedForThisJustice)
summary(model1data$partyStatus)
summary(model1data$logTotalNumCites)

#########################
### STEP 10: OUTLIERS ###
#########################

# Tables A8 and A9 in the Supplemental Appendix

# what cases count as outliers?
# issue needing to be resolved:
#	- data is votes nested in parties nested in issues that we split
# 	- don't want to remove individual observations, need to remove the whole case, for both sides
# 	- how to determine which cases?
#		- Cook's D plots suggest there's significant overlap between the influential outliers and briefs with an outlandish number of citations
#		- so solution is to take out the cases with an outlandish number of citations, period

histogram(model1data$totalNumCites)
boxplot(model1data$totalNumCites)

# what counts as outlandish? Boxplot says anything over 170 total citations is an outlier
# we'll be conservative -- any count over 200 (above 99%)
quantile(model1data$totalNumCites, 0.99)

# comes out to 47 cases
dim(model1data %>% dplyr::select(caseId, totalNumCites) %>% filter(totalNumCites > 200) %>% unique())

# now identify these cases in the OG dataset and then resplit
outliers <- model1data %>% dplyr::select(caseId, totalNumCites) %>% filter(totalNumCites > 200) %>% unique()
outliers <- outliers %>% dplyr::select(-totalNumCites)
outliers <- outliers %>% unique()
outliers$outlier <- 1

# join to model1data
model1dataOut <- left_join(model1data, outliers, by = "caseId")
model1dataOut$outlier <- ifelse(is.na(model1dataOut$outlier), 0, model1dataOut$outlier)

# drop out the outliers
model1dataNoOutliers <- model1dataOut %>% filter(outlier == 0)

# reseparate the data again
model1dataPetNoOut <- model1dataNoOutliers %>% filter(pet == 1)
model1dataRespNoOut <- model1dataNoOutliers %>% filter(pet == 0)

####################################
### STEP 10A: PET OUTLIER MODELS ###
####################################

### TABLE A8, COLUMN 1 ###
model1passivePetNoOut <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPetNoOut)
summary(model1passivePetNoOut)
display(model1passivePetNoOut)

### TABLE A8, COLUMN 2 ###
model1majPetNoOut <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPetNoOut)
summary(model1majPetNoOut)
display(model1majPetNoOut)

### TABLE A8, COLUMN 3 ###
model1separatePetNoOut <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPetNoOut)
summary(model1separatePetNoOut)
display(model1separatePetNoOut)

### TABLE A8 PULL ###
#stargazer(model1passivePetNoOut, model1majPetNoOut, model1separatePetNoOut, align = TRUE, omit.stat=c("LL", "ser", "f"))

#####################################
### STEP 10B: RESP OUTLIER MODELS ###
#####################################

### TABLE A9, COLUMN 1 ###
model1passiveRespNoOut <- lmer(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataRespNoOut)
summary(model1passiveRespNoOut)
display(model1passiveRespNoOut)

### TABLE A9, COLUMN 2 ###
model1majRespNoOut <- lmer(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataRespNoOut)
summary(model1majRespNoOut)
display(model1majRespNoOut)

### TABLE A9, COLUMN 3 ###
model1separateRespNoOut <- lmer(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataRespNoOut)
summary(model1separateRespNoOut)
display(model1separateRespNoOut)

### TABLE A9 PULL ###
#stargazer(model1passiveRespNoOut, model1majRespNoOut, model1separateRespNoOut, align = TRUE, omit.stat=c("LL", "ser", "f"))

################################################
### STEP 11A: PET MODELS - NEGATIVE BINOMIAL ###
################################################

# Tables A10 and A11 in supplemental appendix

### TABLE A10, COLUMN 1 ###
model1passiveNegPet <- glmer.nb(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1passiveNegPet)
display(model1passiveNegPet)

### TABLE A10, COLUMN 2 ###
model1majNegPet <- glmer.nb(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1majNegPet)
display(model1majNegPet)

### TABLE A10, COLUMN 3 ###
model1separateNegPet <- glmer.nb(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataPet)
summary(model1separateNegPet)
display(model1separateNegPet)

### TABLE A10 PULL ###
#stargazer(model1passiveNegPet, model1majNegPet, model1separateNegPet, align = TRUE, omit.stat=c("LL", "ser", "f"))

#############################
### STEP 11B: RESP MODELS ###
#############################

### TABLE A11, COLUMN 1 ###
model1passiveNegResp <- glmer.nb(unnamedCiteCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1passiveNegResp)
display(model1passiveNegResp)

### TABLE A11, COLUMN 2 ###
model1majNegResp <- glmer.nb(majCallOutCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1majNegResp)
display(model1majNegResp)

### TABLE A11, COLUMN 3 ###
model1separateNegResp <- glmer.nb(notMajOpinCount ~ median
						+ ideoAlign
						+ pastExpertise
						+ logAttorneyBriefExperience
						+ clerkedForThisJustice
						+ partyStatus
						+ logTotalNumCites
						+ (1 | issueArea)
						+ (1 | justice)
						+ (1 | term),
						data = model1dataResp)
summary(model1separateNegResp)
display(model1separateNegResp)

### TABLE A11 PULL ###
#stargazer(model1passiveNegResp, model1majNegResp, model1separateNegResp, align = TRUE, omit.stat=c("LL", "ser", "f"))

###############################################################################
### STEP 11: WRITE OUT THE DATA TO RUN THE POOLED CLUSTERED MODELS IN STATA ###
###############################################################################

# write it out into a .dta format
#write_dta(model1dataPet, "~/Dropbox/VanityCitations/Data/Models/Model1PetStataAnalysis20241216.dta")
#write_dta(model1dataResp, "~/Dropbox/VanityCitations/Data/Models/Model1RespStataAnalysis20241216.dta")

# Tables A12 to A17 in the Supplemental Appendix in the Stata File








